package com.dcrzl.shcz.module.sys.controller;

import com.dcrzl.shcz.common.dto.LoginDto;
import com.dcrzl.shcz.common.entity.sys.SysUser;
import com.dcrzl.shcz.common.utils.*;
import com.dcrzl.shcz.module.common.service.CaptchaService;
import com.dcrzl.shcz.module.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 *
 * @author dcrzl
 * @email 1728795786@qq.com
 * @date 2023-04-25 13:39:00
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends BaseController {
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private CaptchaService captchaService;

    /**
     * 用户登录
     *
     * @return
     */
    @PostMapping("/login")
    public Result<Object> login(@RequestBody LoginDto loginDto) {
        //验证图形验证码
        Result validate = captchaService.validate(loginDto.getUuid(), loginDto.getCaptcha());
        if (validate.isSuccess()) {
            String password = loginDto.getPassword();
            String loginName = loginDto.getLoginName();
            SysUser sysUser = new SysUser();
            password = EncryptionUtil.getEncryption("md5", password);
            sysUser.setPassword(password);
            //识别登录名是手机号还是用户名
            if(SysUtils.checkChinaPhoneLegal(loginName)){
                sysUser.setPhone(loginName);
            }else if(SysUtils.checkEmail(loginName)){
                sysUser.setMail(loginName);
            }else{
                sysUser.setUsername(loginName);
            }
            return sysUserService.login(sysUser);
        }
        return new Result<>().setMsg("验证码错误").setSuccess(false);
    }


    /**
    * 列表分页
    */
    @GetMapping("/page")
    public PageInfo<SysUser> page(SysUser sysUser){
        return sysUserService.selectURolePage(sysUser);
    }

    /**
     * 信息
     */
    @GetMapping("/info")
    public Result<SysUser> info(SysUser sysUser){
        sysUser.setId(this.getUserId());
		return sysUserService.selectOne(sysUser);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public Result<SysUser> info(@PathVariable("id") Long id){
        return sysUserService.selectById(id);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public Result<Integer> save(@RequestBody SysUser sysUser){
        sysUser.setUserCode("U"+System.currentTimeMillis());
        sysUser.setPassword(EncryptionUtil.getEncryption("md5",sysUser.getPassword()));
		return sysUserService.insert(sysUser);
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result<Integer> update(@RequestBody SysUser sysUser){
		return sysUserService.updateById(sysUser);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public Result<Integer> delete(@RequestBody Long[] ids){
        //删除
        List<Long> list = new ArrayList<>();
        for (Long sysUserId : ids) {
            Result resultEntity = sysUserService.deleteById(sysUserId);
            if (!resultEntity.isSuccess()){
                list.add(sysUserId);
            }
        }
        if (list.isEmpty()){
            return new Result().setSuccess(true);
        }else{
            return new Result().setSuccess(false).setData(list).setMsg("删除失败").setCode(SysConstant.DELETE_FAIL_CODE_RES);
        }
    }

}
